
import { _decorator, Node, Label, tween, v3, UIOpacity } from 'cc';
import { App } from '../../../Script/App';
import { LayerCfg, LayerBase } from '../../../Script/LayerBase';
const { ccclass, property } = _decorator;

export const ToastCfg: LayerCfg = {
    bundleName: 'Public',
    prefabName: 'Toast'
}

interface ToastData {
    msg: string
    time?: number
}

@ccclass('Toast')
export class Toast extends LayerBase {
    @property(Node)
    private nodeBg!: Node

    @property(Label)
    private label!: Label

    _ret = '1'

    async init(data: ToastData) {
        this.label.string = data.msg
        this._ret = data.msg
        this.scheduleOnce(() => {
            this.close()
        }, data.time || 2.5)

        await App.asyncTween(this.nodeBg, tween().by(0.5, { position: v3(0, 200, 0) }))
        await App.asyncTween(this.nodeBg, tween().delay(1))
        await App.asyncTween(this.nodeBg.getComponent(UIOpacity), tween().to(1, {
            opacity: 0
        }))
    }

    static async show(msg: string) {
        return await App.showLayer(ToastCfg, {
            msg
        })
    }
}
